package com.example.node.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.node.R;
import com.example.node.adapter.MusicListAdapter;
import com.example.node.bean.MusicListBean;
import com.example.node.entity.Constant;
import com.example.node.listener.OnClick;
import com.example.node.listener.OnOkHttpCallback;
import com.example.node.model.MusicListModel;
import com.example.node.model.MusicMoreModel;
import com.example.node.ui.MusicMoreActivity;
import com.example.node.utils.OkHttpUtils;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MusicFragment extends Fragment {

    private RecyclerView mMusicRecyclerView;
    private MusicListAdapter musicListAdapter;
    private List<MusicListBean>mList=new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_music, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mMusicRecyclerView = view.findViewById(R.id.mMusicRecyclerView);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(getActivity(),2);
        mMusicRecyclerView.setLayoutManager(gridLayoutManager);
        musicListAdapter=new MusicListAdapter(mList);
        mMusicRecyclerView.setAdapter(musicListAdapter);
        initMusicList();

        musicListAdapter.setOnClick(new OnClick() {
            @Override
            public void onClick(int position) {
                Intent intent=new Intent(getActivity(), MusicMoreActivity.class);
                MusicListBean musicListBean=mList.get(position);
                intent.putExtra("title",musicListBean.getTitle());
                intent.putExtra("id",musicListBean.getId());
                startActivity(intent);
            }
        });
    }

    private void initMusicList() {
        OkHttpUtils.get(Constant.HTTP_MUSIC_LIST, new OnOkHttpCallback() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onFailure(String meg) {

            }

            @Override
            public void onSuccessful(String json) {
                if (!TextUtils.isEmpty(json)) {
                    parsingMusicListJson(json);
                }
            }
        });
    }

    //解析音乐列表
    private void parsingMusicListJson(String json) {
        Gson gson = new Gson();
        MusicListModel musicListModel = gson.fromJson(json, MusicListModel.class);
        for (int i = 0; i < musicListModel.getData().size(); i++) {
            String musicID = musicListModel.getData().get(i);
            OkHttpUtils.get(Constant.HTTP_MUSIC_MORE + musicID, new OnOkHttpCallback() {
                @Override
                public void onFailure(IOException e) {

                }

                @Override
                public void onFailure(String meg) {

                }

                @Override
                public void onSuccessful(String json) {
                    if (!TextUtils.isEmpty(json)) {
                        parsingMusicMoreJson(json);
                    }
                }
            });
        }
        //在外面去更新列表
    }

    //解析音乐详情
    private void parsingMusicMoreJson(String json) {
        MusicMoreModel musicMoreModel = new Gson().fromJson(json, MusicMoreModel.class);
        String cover=musicMoreModel.getData().getCover();
        String id=musicMoreModel.getData().getId();
        String title=musicMoreModel.getData().getTitle();

        MusicListBean musicListBean=new MusicListBean();
        musicListBean.setId(id);
        musicListBean.setTitle(title);
        musicListBean.setUrl(cover);
        mList.add(musicListBean);
        musicListAdapter.notifyDataSetChanged();
    }
}
